<!-- 自定义标准图表 设置百分百宽度 通过平台组件来适配尺寸 可以替换数据以及设置高级属性自定义扩展 详细查看组件示例.txt
  注:如需设置图片存放文件夹名称需应与当前文件名一致(1并在aircim.webpack.build.component.config.js配置需要打包的文件 2.npm run lib_c 执行打包命令  最后的组件会打包在components/grids中)-->
<template>
  <div class="scale-box" :id="dynamicIdName()">
    <div
      class="service-situation-box animate__animated"
      :class="dynamicClassName"
      style="width: 460px;height: 450px;"
      :id="dynamicChildName()"
    >
      <h1 class="h1_title">
        <span>事项类型分布</span>
      </h1>
      <div class="service-situation">
        <div class="service-situation-item" v-for="(item,index) in itemTypeList" :key="index" :class="{'active': ActiveIndex===index}" @click="handleClickItem(index)">
          <div class="item-title">{{ item.name }}</div>
          <div class="item-content">
            <span class="item-num">{{item.num}}</span>
            <span class="item-unit">个</span>
          </div>
          <div class="item-count">
            占比 <span class="item-per">{{ item.per }}</span> %
          </div>
        </div>
      </div>
      <div class="pie-chart" style="height: 166px;position: relative;margin-top: 36px;">
        <PieChart :echartdata="pieData"></PieChart>
      </div>
    </div>
  </div>
</template>
<script>
//引入封装方法
import encapsulation from "../../utils/aircimGird";
import LineChart from "../elements/LineChart.vue";
import PieChart from "../elements/CustomPieChart.vue";
export default {
  //混入封装方法
  mixins: [encapsulation],
  components: {
    LineChart,
    PieChart,
  },
  data() {
    return {
      resizeObserver: null,
      //动态Id
      timestamp: null,
      ActiveIndex:0,
      itemTypeList:[
        {
          name: "行政权力类事项",
          num: 2684,
          per: "23.40",
          charList:[
            {
              color:'#00D2DF',
              name:'行政许可',
              value:340,
            },
            {
              color:'#B994FF',
              name:'行政确认',
              value:230,
            },
            {
              color:'#85B9FF',
              name:'行政给付',
              value:340,
            },
            {
              color:'#49EEBD',
              name:'行政奖励',
              value:234,
            },
            {
              color:'#F2B073',
              name:'行政裁决',
              value:2340,
            },
            {
              color:'#5576F6',
              name:'其他行政权力',
              value:2340,
            },
          ]
        },
        {
          name: "公共服务类事项",
          num: 2684,
          per: "23.40",
          charList:[
            {
              color:'#00D2DF',
              name:'行政许可',
              value:1230,
            },
            {
              color:'#B994FF',
              name:'行政确认',
              value:1241,
            },
            {
              color:'#85B9FF',
              name:'行政给付',
              value:1140,
            },
            {
              color:'#49EEBD',
              name:'行政奖励',
              value:1240,
            },
            {
              color:'#F2B073',
              name:'行政裁决',
              value:1330,
            },
            {
              color:'#5576F6',
              name:'其他行政权力',
              value:1110,
            },
          ]
        }
      ] // 事项类型列表
    };
  },
  computed:{
    pieData(){
      return this.itemTypeList[this.ActiveIndex].charList;
    }
  },
  mounted() {
    this.timestamp = new Date().getTime();
    this.$nextTick(() => {
      // 监听盒子变化
      this.listenChangeFn();
    });
  },
  methods: {
    handleClickItem(v){
      this.ActiveIndex = v;
    },
    //监听外部盒子的大小 然后对数据井行缩放
    scaleFn() {
      const parentBox = document.getElementById(`dynamic-${this.timestamp}`);
      const childBox = document.getElementById(
        `dynamicChild-${this.timestamp}`
      );
      const parentWidth = parentBox.offsetWidth;
      const parentHeight = parentBox.offsetHeight;
      const childWidth = childBox.offsetWidth;
      const childHeight = childBox.offsetHeight;
      let scaleX, scaleY;
      if (parentWidth < childWidth || parentHeight < childHeight) {
        const scaleBasedOnWidth = parentWidth / childWidth;
        const scaleBasedOnHeight = parentHeight / childHeight;
        scaleX = scaleBasedOnWidth;
        scaleY = scaleBasedOnHeight;
      } else {
        scaleX = parentWidth / childWidth;
        scaleY = parentHeight / childHeight;
      }
      childBox.style.transform = `scale(${scaleX}, ${scaleY})`;
    },
    // 监听盒子变化
    listenChangeFn() {
      let that = this;
      const element = document.getElementById(`dynamic-${this.timestamp}`);
      // console.log(element, "element");
      if (element) {
        new ResizeObserver(() => {
          // 处理元素大小变化的逻辑
          that.scaleFn();
        }).observe(element);
      }
    },
    // 绑定动画样式 animate__flash
    bindAnimationFn(animateName) {
      if (animateName != null) {
        this.dynamicClassName = animateName;
      }
    },
    //定义唯一ID名
    dynamicIdName() {
      return `dynamic-${this.timestamp}`;
    },
    //定义唯一ID名
    dynamicChildName() {
      return `dynamicChild-${this.timestamp}`;
    },
  },
  // 销毁监听
  beforeDestroy() {
    // 组件销毁前停止监听
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
    }
    if (this.requestTime != null) {
      clearInterval(this.requestTime);
    }
  },
};
</script>
<style lang="scss" scoped>
@import "../../static/assets/common.scss";
</style>
<style lang="scss" scoped>
.scale-box {
  width: 100%;
  height: 100%;
}

.service-situation-box {
  position: absolute;
  padding: 0 20px;
  box-sizing: border-box;
  overflow: hidden;
  transform-origin: left top;
  background-image: url(../../../thumbnail/backImg/new-border-green.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  color: #fff;
}
.service-situation {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  &-item {
    position: relative;
    margin-bottom: 20px;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    width: 197px;
    height: 124px;
    background: url(../../static/assets/ServiceSituation/service_item_bg.png)
      no-repeat center center;
    background-size: cover;
    text-align: center;
    &.active{
      background-image: url("../../static/assets/ServiceSituation/service_item_bg_a.png");
      &::after{
        position: absolute;
        content:"";
        left: 50%;
        transform: translateX(-50%);
        bottom: -32px;
        width: 30px;
        height: 27px;
        background: url("../../static/assets/ServiceSituation/arrow.png") no-repeat center center;
        background-size: cover;
      }
    }
    .item{
      &-title{
        display: flex;
        justify-content: center;
        align-items: center;
        height: 38px;
        color: #FFF;
        font-family: "Source Han Sans CN";
        font-size: 16px;
        font-weight: 500;
      }
      &-content{
        display: flex;
        justify-content: center;
        align-items: center;
        height: 48px;
      }
      &-num{
        color: #FFF;
        text-align: center;
        font-family: D-DIN;
        font-size: 24px;
        font-weight: 700;
      }
      &-unit{
        color: #96BEE0;
        font-family: "Source Han Sans CN";
        font-size: 16px;
        font-weight: 400;
        margin-left: 4px;
      }
      &-count{
        display: flex;
        justify-content: center;
        align-items: center;
        height: 28px;
        color: #96BEE0;
        font-family: "Source Han Sans CN";
        font-size: 14px;
        font-weight: 400;
      }
      &-per{
        margin: 0 4px 0 10px;
        color: #FFF;
        font-family: D-DIN;
        font-size: 20px;
        font-weight: 700;
      }
    }
  }
}


</style>
